import { _decorator, Component, Input, Node, Sprite, tween, Tween, Vec3 } from 'cc';
import { GAME_POP, GAME_SPRITEFRAME, GlobalData, LevelConfig } from '../../../Data/GlobalData';
import { DragonLoad } from './DragonLoad';
import { Tools } from '../../../Tool/Tools';
const { ccclass, property } = _decorator;

@ccclass('DragonBody')
/**
 * 龙的身体
 */
export class DragonBody extends Component {

    //身体
    @property(Sprite)
    private Body: Sprite = null;

    //翅膀
    @property(Node)
    private Wing: Node = null;

    //身体索引
    public BodyIndex: number = 0;

    //移动索引
    public MoveIndex: number = 0;

    //后退速度
    public BackSpeed: number = 0;

    //当前已经移动的位置
    public CurrentDistance: number = 0;

    //
    public DragonLoad: DragonLoad = null;

    //
    public IsHit: boolean = true;

    protected start(): void {

    }



    /**
     * 我消失
     */
    public IamDisappear() {
        this.DragonLoad.CheckDisappear(this.node);
    }

    /**
     * 更新身体样式
     */
    public UpdateBodyStyle(_index: number) {
        this.IsHit = true;
        this.BodyIndex = _index;
        this.MoveIndex = 0;
        this.MoveFollow(0);
        this.Body.spriteFrame = app.resManager.SpriteFrames[GAME_SPRITEFRAME.DragonBody + this.BodyIndex];
        this.Wing.active = false;
        if (_index == 99) {
            console.log("更新了么")
            this.Body.spriteFrame = app.resManager.SpriteFrames[GAME_SPRITEFRAME.DragonHead + app.gameData.GameData.basket];
            this.Wing.getComponent(Sprite).spriteFrame = app.resManager.SpriteFrames[GAME_SPRITEFRAME.Wing + app.gameData.GameData.basket];
            this.PlayWingAni();
        }

        this.CurrentDistance = 0;
    }

    /**
     * 播放翅膀动画
     */
    private PlayWingAni() {
        if (this.BodyIndex != 99) return;
        this.Wing.active = true;
        Tween.stopAllByTarget(this.Wing);
        this.Wing.angle = -30;
        tween(this.Wing).to(0.3, { angle: 30 }).to(0.3, { angle: -30 }).call(() => {
            this.PlayWingAni();
        }).start();

    }

    /**
     * 更新位置和旋转
     */
    public UpdateTransform(_pos: Vec3, _angle: number) {
        this.node.setPosition(_pos.x, _pos.y);
        this.node.setScale(_angle, 1, 1);
    }

    /**
     * 自己移动
     */
    public MoveSelf(_dt: number) {
        if (this.CurrentDistance >= app.pathManager.MaxDistance) {//游戏结束
            this.DragonLoad.GameScene.GameOver();
            return;
        }
        this.CurrentDistance += (LevelConfig[GlobalData.CurrentLevel].speed + GlobalData.BodyAddSpeed) * _dt;
        this.MoveFollow(app.pathManager.findIndexByDistance(this.CurrentDistance));
    }

    /**
     * 更新距离
     */
    public UpdateDistance(_distance: number) {
        this.CurrentDistance = _distance;
        if (this.CurrentDistance <= 0) {
            return;
        }
        this.MoveFollow(app.pathManager.findIndexByDistance(this.CurrentDistance));
    }

    /**
     * 后退
     */
    public MoveBack(_dt: number) {
        this.CurrentDistance -= this.BackSpeed * _dt;
        if (this.CurrentDistance <= 0) {
            return;
        }
        this.MoveFollow(app.pathManager.findIndexByDistance(this.CurrentDistance));

    }

    /**
     * 我跟随移动
     */
    public MoveFollow(_moveindex: number) {
        if (_moveindex < 0) {
            // this.UpdateTransform(new Vec3(1000, 1000), 0);
            return;
        }

        //更新我移动的索引
        this.MoveIndex = _moveindex;

        // 获取当前位置和角度
        const entry = app.pathManager.DistanceTable[this.MoveIndex];
        const curve = app.pathManager.Cuvers[entry.curveIndex];
        const position = app.pathManager.getPointOnCurve(curve, entry.t);
        const angle = app.pathManager.isNextXGreater(curve, entry.t) ? 1 : -1;
        this.UpdateTransform(new Vec3(position.x, position.y), angle);
    }

    /**
     * 返回我是否可以碰撞
     */
    public ReturnCanHit(): boolean {
        return this.IsHit && this.node.getPosition().y < (400+GlobalData.FireRange);
    }
}


